package com.face.receive.jsu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.face.bean.jsu.FindPopulation;
import com.face.operate.jsu.Select;
import com.face.sql.jsu.FindDao;

@WebServlet("/select")
public class SelectByLocationAndTime extends HttpServlet {

	protected void doGet(HttpServletRequest request,HttpServletResponse response)
	    throws ServletException,IOException{
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request,HttpServletResponse response)
	    throws ServletException,IOException{
		String province = request.getParameter("province");
	
		String city = request.getParameter("city");
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		
		JSONArray array = new JSONArray();
		Select select = new Select();
		if(city.equals("")){
		List<FindPopulation> list =	select.selectByProvince(province,start,end);
		for(FindPopulation find:list){
			JSONObject json = new JSONObject();
			
				try {
					json.put("url", find.getFacepath());
					String thecity = find.getCity();
					System.out.println(thecity);
					if(thecity.equals("null")){
					   json.put("location", find.getProvince()+find.getLocation());
					}else{
						json.put("location", find.getProvince()+find.getCity()+find.getLocation());
					}
					json.put("time", find.getTimestamp());
					json.put("message", find.getMessage());
					array.put(json);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			
			System.out.println(find);
		}
		}else{
			List<FindPopulation> list =	select.selectByCity(province,city,start,end);
			for(FindPopulation find:list){
				JSONObject json = new JSONObject();
				try {
					json.put("url", find.getFacepath());
					json.put("location", find.getProvince()+find.getCity()+find.getLocation());
					json.put("time", find.getTimestamp());
					json.put("message", find.getMessage());
					array.put(json);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
		response.setContentType("application/json; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(array.toString());
		System.out.println("返回的json数据：" + array.toString());
	}
}
